/**
 *
 */
package com.zhengtoon.mctoon.conv.service;

import com.qitoon.framework.core.exception.BusinessException;
import com.zhengtoon.mctoon.conv.bean.ConvComment;
import com.zhengtoon.mctoon.conv.bean.ConvRemindRecord;
import com.zhengtoon.mctoon.conv.dao.ConvCommentMapper;
import com.zhengtoon.mctoon.conv.dao.ConvPhotoInfoMapper;
import com.zhengtoon.mctoon.conv.dao.ConvRemindRecordMapper;
import com.zhengtoon.mctoon.conv.dto.CommentInfoDto;
import com.zhengtoon.mctoon.conv.dto.CommentListResultItemDto;
import com.zhengtoon.mctoon.conv.dto.ResultCommentDto;
import com.zhengtoon.mctoon.conv.dto.mybatis.Page;
import com.zhengtoon.mctoon.conv.exception.ConvPhotoExceptionCode;
import com.zhengtoon.mctoon.conv.service.ConvCommentService;
import com.zhengtoon.mctoon.conv.utils.AuditCommentUtils;
import com.zhengtoon.mctoon.conv.utils.DateFormatAppUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 评论 impl实现
 *
 * @author 赵康峰
 * @since 2017-05-26
 * Copyright: Copyright (c) 2017
 * Company:北京政务通科技有限公司
 */
@Service
public class ConvCommentServiceImpl implements ConvCommentService {

    private static Logger log = LoggerFactory.getLogger(ConvCommentServiceImpl.class);

    @Autowired
    private ConvCommentMapper convCommentMapper;

    @Autowired
    private ConvRemindRecordMapper convRemindRecordMapper;

    @Override
    public List<ResultCommentDto> queryCommentListById(String id, String pageNum, String pageSize, String userId) {

        Integer num = Integer.valueOf(pageNum);
        Integer size = Integer.valueOf(pageSize);
        List<ConvComment> list = convCommentMapper.getCommentListById((num - 1) * size, size, Long.valueOf(id), Long.valueOf(userId));
        List<ResultCommentDto> resultCommentDtoList = new ArrayList<>();
        for (ConvComment convComment : list) {
            ResultCommentDto resultCommentDto = new ResultCommentDto();
            resultCommentDto.setFeedId(convComment.getFeedId());
            resultCommentDto.setCommentAvator(convComment.getCommentAvator());
            resultCommentDto.setCommentContent(convComment.getParamContent());
            resultCommentDto.setCommentName(convComment.getCommentName());
            resultCommentDto.setCommentTime(DateFormatAppUtils.formatDateTime(convComment.getUpdateTime()));
            resultCommentDtoList.add(resultCommentDto);
        }
        return resultCommentDtoList;
    }

    @Transactional
    @Override
    public int addComment(CommentInfoDto commentInfoDto) {
        ConvComment convComment = new ConvComment();
        convComment.setConvId(Long.valueOf(commentInfoDto.getId()));
        convComment.setUserId(Long.valueOf(commentInfoDto.getUserId()));
        convComment.setFeedId(commentInfoDto.getFeedId());
        convComment.setCommentAvator(commentInfoDto.getCommentAvator());
        convComment.setCommentName(commentInfoDto.getCommentName());
        convComment.setParamContent(commentInfoDto.getContent());
        convComment.setCreateTime(new Date().getTime());
        convComment.setUpdateTime(new Date().getTime());
        convCommentMapper.addComment(convComment);
        int result = AuditCommentUtils.auditComment(commentInfoDto.getContent(), String.valueOf(convComment.getAppraiseId()));
        if (result == 0) {
            throw new BusinessException(ConvPhotoExceptionCode.AUDIT_COMMENT_IS_FAIL);
        }
        return result;
    }

    @Override
    public Page queryCommentList(Page page) {

        try {
            // 1.查询状态为举报的随后拍记录, 未锁定的

            // 2.分页,条件查询

            // 查询数据
            List<ConvComment> convPhotoInfoList = convCommentMapper.queryCommentList(page);

            // 查询数据条数
            Integer dataTotal = convCommentMapper.queryCommentListCount(page);

            //  参数转换,处理
            List<CommentListResultItemDto> resultItemDtoList = convertToCommentListResultItemDto(convPhotoInfoList);

            // 返回参数
            if (dataTotal == null) {
                dataTotal = 0;
            }

            page.setResult(resultItemDtoList);
            page.setDataTotal(dataTotal);

            return page;
        } catch (Exception e) {
            log.error("查询举报列表失败", e);
            throw new BusinessException(ConvPhotoExceptionCode.REPORT_LIST_ERROR);
        }
    }

    /**
     * 显示/删除评论  144637
     *
     * @param id
     * @param isPublic
     * @return
     */
    @Override
    public boolean delComment(Long id, Integer isPublic) {

        try {
            int row = convCommentMapper.delComment(id, isPublic);
            return row == 1;
        } catch (Exception e) {
            log.error("显示/删除评论失败", e);
            throw new BusinessException(ConvPhotoExceptionCode.DELETE_COMMENT_ERROR);
        }
    }

    @Override
    @Transactional
    public int updateCommentPass(String appraiseId) {
        convCommentMapper.updateCommentPass(Long.valueOf(appraiseId));
        ConvComment convComment = convCommentMapper.queryConvId(Long.valueOf(appraiseId));
        ConvRemindRecord convRemindRecord = convRemindRecordMapper.queryRemind(convComment.getConvId());
        ConvRemindRecord updateConvRemindRecord = new ConvRemindRecord();
        updateConvRemindRecord.setRemindId(convRemindRecord.getRemindId());
        updateConvRemindRecord.setCommentCount(convRemindRecord.getCommentCount() + 1);
        updateConvRemindRecord.setUpdateTime(new Date().getTime());
        return convRemindRecordMapper.updateRemind(updateConvRemindRecord);
    }

    @Override
    public int updateCommentFail(String appraiseId) {

        return convCommentMapper.updateCommentFail(Long.valueOf(appraiseId));
    }

    private List<CommentListResultItemDto> convertToCommentListResultItemDto(List<ConvComment> convComments) {

        List<CommentListResultItemDto> dtoList = new ArrayList<>();

        for (ConvComment item : convComments) {

            CommentListResultItemDto dto = new CommentListResultItemDto();
            dto.setAppraiseId(item.getAppraiseId());
            dto.setCardName(item.getCommentName());
            dto.setCommentContent(item.getParamContent());
            dto.setIsPublic(item.getDeleteFlag());
            dto.setCreateTime(DateFormatAppUtils.getFormatDate(item.getCreateTime()));

            dtoList.add(dto);
        }

        return dtoList;
    }

}